class Sample:
    def __enter__(self):
        print('enter')
        return self

    def __exit__(self, *args):
        print('exit')

    def do_something(self):
        print('do_something')


if __name__ == '__main__':
    s = Sample()
    s.do_something()
    with Sample() as sample:
        sample.do_something()
"""
结果：
    do_something
    enter
    do_something
    exit
    
---->类中使用魔法函数__enter__和__exit__后，就满足上下文管理协议，就可以使用with  as 语句
"""
